package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.bean.Param;
import com.efast.dmom.mes.bean.ParamGroup;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.datahandlers.entity.ParamGroupHandler;
import com.efast.dmom.mes.datahandlers.entity.ParamHandler;
import com.efast.dmom.mes.entity.ParamEntity;
import com.efast.dmom.mes.service.ParamGroupService;

import jodd.util.StringUtil;

@Service
public class ParamGroupServiceImpl implements ParamGroupService {

	@Autowired
	private ParamGroupHandler paramGroupHandler;
	@Autowired
	private ParamHandler paramHandler;
	@Override
	public ResultBean save(ParamGroup paramGroup) {
		// TODO Auto-generated method stub
		ResultBean resultBean = new ResultBean(true, ' ');
		if(paramGroup==null || paramGroup.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		String name = paramGroup.getInnerData().getName();
		int isGlobal = paramGroup.getInnerData().getIsGlobal();
		long plantId = paramGroup.getInnerData().getPlantId();
		if(!StringUtil.isNotBlank(name)) {
			resultBean.setSuccess(false);
			resultBean.setMessage("参数组名称不能为空!");
			return resultBean;
		}
		if(isGlobal==0) {
			if(plantId==0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("非全局时工厂信息为必填项!");
				return resultBean;
			}
		}
		ParamGroup byNameAndPlantIdAndGlobal = paramGroupHandler.getByNameAndPlantIdAndGlobal(name, plantId, isGlobal);
		if(byNameAndPlantIdAndGlobal.getInnerData()!=null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("该参数组配置已经存在！");
			return resultBean;
		}
		paramGroupHandler.save(paramGroup);
		resultBean.setSuccess(true);
		resultBean.setMessage("保存成功!");
		return resultBean;
	}

	@Override
	public ResultBean update(ParamGroup paramGroup) {
		// TODO Auto-generated method stub
		ResultBean resultBean = new ResultBean(true, ' ');
		if(paramGroup==null || paramGroup.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		String name = paramGroup.getInnerData().getName();
		int isGlobal = paramGroup.getInnerData().getIsGlobal();
		long plantId = paramGroup.getInnerData().getPlantId();
		if(!StringUtil.isNotBlank(name)) {
			resultBean.setSuccess(false);
			resultBean.setMessage("参数组名称不能为空!");
			return resultBean;
		}
		if(isGlobal==0) {
			if(plantId==0) {
				resultBean.setSuccess(false);
				resultBean.setMessage("非全局时工厂信息为必填项!");
				return resultBean;
			}
		}
		ParamGroup byId = paramGroupHandler.getById(paramGroup.getInnerData().getId());
		if(byId.getInnerData() ==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		if(!name.equals(byId.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("参数组名称不可修改!");
			return resultBean;
		}
		if(!name.equals(byId.getInnerData().getName()) ||plantId !=byId.getInnerData().getPlantId() || isGlobal != byId.getInnerData().getIsGlobal()) {
			ParamGroup byNameAndPlantIdAndGlobal = paramGroupHandler.getByNameAndPlantIdAndGlobal(name, plantId, isGlobal);
			if(byNameAndPlantIdAndGlobal.getInnerData()!=null) {
				resultBean.setSuccess(false);
				resultBean.setMessage("该参数组配置已经存在！");
				return resultBean;
			}
		}
		paramGroupHandler.update(paramGroup);
		resultBean.setSuccess(true);
		resultBean.setMessage("修改成功!");
		return resultBean;
	}

	@Override
	public ResultBean delete(ParamGroup paramGroup) {
		ResultBean resultBean = new ResultBean(true, ' ');
		if(paramGroup==null || paramGroup.getInnerData()==null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("信息为空!");
			return resultBean;
		}
		Param param = new Param();
		ParamEntity paramEntity = new ParamEntity();
		paramEntity.setParamGroupId(paramGroup.getInnerData().getId());
		param.setInnerData(paramEntity);
		paramHandler.deleteByParamGroupId(param);
		
		paramGroupHandler.delete(paramGroup);
		
		resultBean.setSuccess(true);
		resultBean.setMessage("删除成功!");
		return resultBean;
	}

	@Override
	public ResultBean deleteCheck(List<ParamGroup> listParamGroup) {
		// TODO Auto-generated method stub
		ResultBean resultBean = new ResultBean(true, ' ');
		if(listParamGroup !=null && listParamGroup.size()>0) {
			for(ParamGroup paramGroup:listParamGroup ) {
				 List<Param> paramGroupList = paramHandler.getByParamGroupId(paramGroup.getInnerData().getId());
				 if(paramGroupList!= null && paramGroupList.size()>0) {
						resultBean.setSuccess(false);
						resultBean.setMessage("参数组:"+paramGroup.getInnerData().getName()+"存在子项");
						return resultBean;
				 }
								
				}
			}
		return resultBean;
	}

}
